package com.springboot_gradrecruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.intention.AddIntentionRequest;
import com.springboot_gradrecruit.model.dto.intention.PageIntentionRequest;
import com.springboot_gradrecruit.model.dto.intention.UpdateIntentionRequest;
import com.springboot_gradrecruit.model.dto.job.PageJobRequest;
import com.springboot_gradrecruit.model.entity.Intention;
import com.springboot_gradrecruit.model.vo.intention.IntentionVO;
import com.springboot_gradrecruit.service.IntentionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zr
 */
@Tag(name = "求职意向相关接口")
@RestController
@RequestMapping("/intention")
@Slf4j
public class IntentionController {

    @Resource
    private IntentionService intentionService;

    @Operation(summary = "添加求职意向")
    @PostMapping("/creatIntention")
    public R<Boolean> addIntention(@RequestBody AddIntentionRequest addIntentionRequest, HttpServletRequest request) {
        //1. 参数校验
        if (addIntentionRequest == null || request == null) {
            return R.error(null, "参数为空");
        }
        //2。. 调用service完成添加
        Long aLong = intentionService.creatIntention(addIntentionRequest, request);
        if(aLong>0){
            return R.success(true, "添加成功");
        }
        return R.error(false, "添加失败");
    }

    @Operation(summary = "查询求职意向根据求职意向绑定的用户ID")
    @PostMapping("/queryIntentionByUserId")
    public R<IntentionVO> queryIntentionByUserId(Long intentionUserId) {
        //1. 参数校验
        if (intentionUserId == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        QueryWrapper<Intention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("intention_user_id", intentionUserId);
        Intention intention = intentionService.getOne(queryWrapper);
        IntentionVO intentionVO = BeanUtil.copyProperties(intention, IntentionVO.class);
        return R.success(intentionVO, "查询成功");


    }

    //更新求职意向
    @Operation(summary = "更新求职意向")
    @PostMapping("/updateIntentionById")
    public R<Boolean> updateIntentionById(@RequestBody UpdateIntentionRequest updateIntentionRequest) {
        //1. 参数校验
        if (updateIntentionRequest == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成更新
        boolean updateIntention = intentionService.updateIntentionById(updateIntentionRequest);
        if(updateIntention){
            return R.success(null, "修改成功");
        }
        return R.error(null, "修改失败");
    }

    //分页获取求职意向
    @Operation(summary = "分页获取求职意向")
    @PostMapping("/pageIntention")
    public R<PageResponse<IntentionVO>> pageIntention(@RequestBody PageIntentionRequest pageIntentionRequest) {

        //1. 参数校验
        if (pageIntentionRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        PageResponse<IntentionVO> pageResponse=intentionService.pageIntention(pageIntentionRequest);
        return R.success(pageResponse, "查询成功");
    }


    //根据企业的发布的岗位标题查询对应的求职意向，查询出简历
    @Operation(summary = "根据企业的发布的岗位标题查询对应的求职意向，查询出简历")
    @PostMapping("/queryIntentionByJobTitle")
    public R<List<IntentionVO>> queryIntentionByJobTitle(@RequestBody List<String> jobTitle) {
        //1. 参数校验
        if (jobTitle == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        List<IntentionVO> intentionVOS = intentionService.queryIntentionByJobTitle(jobTitle);
        return R.success(intentionVOS, "查询成功");
    }

    //根据企业的发布的岗位行业查询对应的求职意向，查询出简历
    @Operation(summary = "根据企业的发布的岗位行业查询对应的求职意向，查询出简历")
    @PostMapping("/queryIntentionByJobIndustry")
    public R<List<IntentionVO>> queryIntentionByJobIndustry(@RequestBody List<String> jobIndustry) {
        //1. 参数校验
        if (jobIndustry == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        List<IntentionVO> intentionVOS = intentionService.queryIntentionByJobIndustry(jobIndustry);
        return R.success(intentionVOS, "查询成功");
    }


}
